package com.itcg.springboot01change.controller;

import com.itcg.springboot01change.pojo.Account;
import com.itcg.springboot01change.pojo.AccountTransfer;
import com.itcg.springboot01change.pojo.Page;
import com.itcg.springboot01change.pojo.Result;
import com.itcg.springboot01change.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    //添加账户
    @RequestMapping("/insert")
    public Result insert(@RequestBody Account account) {
        if (null == account || account.equals("")) {
            return Result.error("参数不能为空");
        }
        if (accountService.selectByName(account.getName()) != null) {
            return Result.error("数据存在,请重新输入");
        }
        accountService.insert(account);
        return Result.success();
    }

    //根据 id 删除账户
    @RequestMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        //在排断id存不存在
        Account account = accountService.selectById(id);
        if (account != null) {
            accountService.delete(id);
            return Result.success("删除成功");
        }
        return Result.error("数据不存在");
    }

    //根据 id 修改账户
    @RequestMapping("/update")
    public Result update(@RequestBody Account account) {
        accountService.update(account);
        return Result.success();

    }

    //查询所有账户
    @RequestMapping("/selectAll")
    public Result selectAll() {
        List<Account> accountList = accountService.selectAll();
        return accountList.isEmpty() ? Result.error("目前没有数据") : Result.success(accountList);
    }

    //根据id查询账户
    @RequestMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error(" id 参数不合法");
        }
        Account account = accountService.selectById(id);
        if (account == null) {
            return Result.error("该数据不存在");
        } else {
            return Result.success(account);
        }
    }

    //根据 selectByName 查询账户
    @RequestMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable("name") String name) {
        if (null == name || name.equals("")) {
            return Result.error("name 参数不合法");
        }
        Account account = accountService.selectByName(name);
        if (account == null) {
            return Result.error("没有该数据");
        }
        return Result.success(account);
    }

    //转账
    @RequestMapping("/transfer")
    public Result transfer(@RequestBody AccountTransfer accountTransfer) {
        String inName = accountTransfer.getInName();
        String outName = accountTransfer.getOutName();
        Double money = accountTransfer.getMoney();
        if (inName == null || outName == null || money == null) {
            return Result.error("参数格式错误");
        }
        try {
            accountService.transfer(inName, outName, money);
            return Result.success();
        } catch (Exception e) {
            return Result.error("转账失败");
        }
    }

    //@RequestMapping("/findPage")
    //public Page<Account> findPage(String homeAddress,
    //                              @RequestParam(defaultValue = "1") Integer pageNum,
    //                              @RequestParam(defaultValue = "3") Integer pageSize) {
    //    Page<Account> page = new Page<>();
    //    List<Account> accounts = accountService.findAddressList(homeAddress);
    //    page.setDataList(accounts);
    //    page.setPageNum(pageNum);
    //    page.setPageSize(pageSize);
    //    return page;
    //}
}
